<template>
  <div class="list-table-box mt12">
    <div class="list-caption f16" style="padding-left: 12px">稿件列表</div>

    <CommonTablePage
      :loading="tableLoading"
      :columns="columns"
      :tableData="tableData"
      :total="pagination.total"
      :current="formData.pageNo"
      @on-selection-change="handleTableSelect"
      @on-change="handlePageChange"
      @on-page-size-change="handlePageSizeChange"
      @on-sort-change="handleTableSort"
    ></CommonTablePage>
  </div>
</template>

<script>
import CommonTablePage from '@/components/common/CommonTablePage/CommonTablePage'

// util
import { createCommonPagination } from '@/utils/service.js'
import { createColumns } from './cityTopic'
import $bus from '@/utils/bus.js'

import mixin from './mixin'
export default {
  name: 'CityTopic',

  mixins: [mixin],

  components: { CommonTablePage },

  props: {
    formData: { type: Object },
  },

  data() {
    return {
      tableLoading: false,
      columns: createColumns(this),
      tableData: [],
      orderField: 'loadTime',
      orderDesc: true,
      pagination: createCommonPagination(100),
    }
  },

  methods: {
    handleTableSelect(selection) {
      this.selection = selection
      this.$emit('on-selection-change', selection)
    },

    handleBatchEnterPool() {
      if (!this.selection) return
      const ids = this.selection.map(s => s.id).join()
      const p = { ids, type: this.formData.type }
      this.postPooling(p)
    },

    handlePageChange(page) {
      this.formData.pageNo = page
      this.handleFormSubmit(true)
    },

    handlePageSizeChange(pageSize) {
      this.formData.pageSize = pageSize
      this.handleFormSubmit(true)
    },

    handleTableSort({ column, key, order }) {
      // console.log(123, column, key, order)
      if (order === 'normal') return
      this.orderDesc = order === 'desc'
      this.orderField = key
      this.handleFormSubmit()
    },

    handleFormSubmit(no_reset) {
      if (no_reset !== true) {
        this.formData.pageNo = 1
      }
      const p = { ...this.formData }
      p.desc = this.orderDesc
      p.orderField = this.orderField
      this.getRecommendList(p)
    },

    enterPool(item) {
      if (item.pooling) return
      const p = { ids: item.id, type: this.formData.type }
      this.postPooling(p)
    },
  },

  mounted() {
    $bus.$on('on-recomment-list', this.handleFormSubmit)
    $bus.$on('on-recomment-batch-enter', this.handleBatchEnterPool)
    this.handleTableSelect([])
    this.$nextTick(this.handleFormSubmit)

    /* while (this.tableData.length < 15) {
      this.tableData.push({ test: true })
    } */
  },

  beforeDestroy() {
    $bus.$off()
  },
}
</script>

<style lang="less" scoped></style>
